define(function(require,exports,module) {

    var oAutoPlayBox = '';                  // 倒计时进度容器
    var oNextTimeProcess = '';              // 倒计时经度
    var oUserSelet = $('.js_userSelet');            // 选集
    var oVideo_miaoshu = $('.js_video_miaoshu');    // 描述


    function videoSet(sObj) {
        var videoTest = new videojs(sObj,{
            techOrder : ["html5"],
            loadingSpinner: true,
            bigPlayButton : true,
            textTrackDisplay : false,
            posterImage: false,
            errorDisplay : false,
            textTrackSettings: false,
            controlBar : {
                captionsButton : false,
                chaptersButton: false,
                subtitlesButton: false,
                liveDisplay: false,
                volumePanel: false,
                remainingTimeDisplay: true
            },

        },function() {
            var _this = this;
            var $this = $(this.el_);
            // console.log('.js_playVideo-' + sObj)
            var playerVideo = $this.parents('.js_playVideo');                       // 播放器容器
            var playerLayer = playerVideo.find('.js_player-layer');                 // 播放器遮罩
            var playerRepeat = playerVideo.find('.js_player-repeat');               // 重播按钮
            var PlayerErrorTip = playerVideo.find('.js_player-errorTip');           // 错误提醒
            var vjsErrorDisplay = playerVideo.find('.vjs-error-display');           // video本身的错误提醒

            var oBigPlayButton = $this.find('.vjs-big-play-button');                // 播放按钮大


            oAutoPlayBox = playerVideo.find('.js_autoPlayBox');                 // 倒计时进度容器
            oNextTimeProcess = playerVideo.find('.js_nextTimeProcess');         // 倒计时经度


            var aTiptext = ['自动播放下一集，点击可取消', '自动播放下一集，点击开启'];


            // 数据开始加载
            _this.on('loadstart',function(){
                // console.log('loadstart')
            });

            // 视频播放
            _this.on('play',function(){
                playerLayer.hide();
                oBigPlayButton.hide();
                playerRepeat.hide();
                PlayerErrorTip.hide();

                videoEvent.checkAutoPlayNext(function () {
                    videoEvent.autoPlayTipClose(true);
                });

            });

            // 数据加载完成
            _this.on('loadeddata',function(){
                // console.log('loadeddata')
            });

            // 视频暂停
            _this.on('pause',function(){
                // console.log('pause');
                oBigPlayButton.show();
            });

            // 视频播放结束
            _this.on('ended',function(){
                // console.log('ended');
                oBigPlayButton.hide();
                playerRepeat.show();
                videoEvent.checkAutoPlayNext(function () {
                    videoEvent.autoPlayTip(function () {
                        // 自动下一集
                        var _indexNext = oUserSelet.find('li.active').index() + 1;
                        if(_indexNext >= oUserSelet.find('li').length-1) return;
                        oUserSelet.find('li').removeClass('active').eq( _indexNext ).addClass('active');
                        videoEvent.VideChange(_indexNext, videoTest, $this, function () {
                            playerLayer.hide();
                            oBigPlayButton.show();
                            playerRepeat.hide();
                        }, function () {
                            ErrorTip();
                        });
                    });
                });

            });

            // 重播
            playerRepeat.find('.js_replayBtn').on('click', function () {
                videoTest.play();
                playerRepeat.hide();
            });

            // 开启和关闭自动播放
            oAutoPlayBox.on('click', function () {
                if(oAutoPlayBox.hasClass('active')) {
                    oAutoPlayBox.removeClass('active').find('.js_tip').text(aTiptext[0]);
                    videoEvent.autoPlayTip(function () {
                        // 自动下一集
                        var _indexNext = oUserSelet.find('li.active').index() + 1;
                        if(_indexNext >= oUserSelet.find('li').length-1) return;
                        oUserSelet.find('li').removeClass('active').eq( _indexNext ).addClass('active');
                        videoEvent.VideChange(_indexNext, videoTest, $this, function () {
                            playerLayer.hide();
                            oBigPlayButton.show();
                            playerRepeat.hide();
                        }, function () {
                            ErrorTip();
                        });
                    });
                }
                else {
                    oAutoPlayBox.addClass('active').find('.js_tip').text(aTiptext[1]);
                    videoEvent.autoPlayTipClose();
                }
            });


            // 选集
            oUserSelet.on('click', 'li', function () {
                var index = $(this).index();
                oUserSelet.find('li').removeClass('active').eq(index).addClass('active');
                videoEvent.VideChange(index, videoTest, $this, function () {
                    playerLayer.hide();
                    oBigPlayButton.show();
                    playerRepeat.hide();
                }, function () {
                    ErrorTip();
                })
            });


            // 错误提醒
            function ErrorTip() {
                if(vjsErrorDisplay.length > 0) vjsErrorDisplay.hide();
                PlayerErrorTip.show();
                console.log(_this.error().code)
                switch (_this.error().code) {
                    case 0:
                        PlayerErrorTip.find('.errorTip').text('');
                        break;
                    case 1:
                        PlayerErrorTip.find('.errorTip').text('视频播放已经停止！');
                        break;
                    case 2:
                        PlayerErrorTip.find('.errorTip').text('视频下载失败！稍后再试！');
                        break;
                    case 3:
                        PlayerErrorTip.find('.errorTip').text('浏览器不兼容，无法正常播放！');
                        break;
                    case 4:
                        PlayerErrorTip.find('.errorTip').text('视频资源有误，无法正常播放！');
                        break;
                    case 5:
                        PlayerErrorTip.find('.errorTip').text('视频解密有误，无法正常播放！');
                        break;
                    default:
                        break;
                }
            }

        });
    }

    // 视频自动播放操作
    var videoEvent = {
        nTime: 5,
        errorTime: null,
        // 换集
        VideChange: function(index, video, $video, fn, error) {
            var _this = this
            var oLi = oUserSelet.find('li');
            // 最后一集处理
            if(index >= oLi.length-1)  return;
            var sourceSrc =  oLi.eq(index).attr('videoHref');
            var sourceDescription = oLi.eq(index).attr('videodescription');
            var sourceVideoImg = oLi.eq(index).attr('videoImg');
            var sourceVideoId = oLi.eq(index).attr('videoId');
            // playerRepeat.css('background-image','url('+sourceVideoImg+')');
            oVideo_miaoshu.text(sourceDescription);
            if($video.attr('_index') != index) {
                $video.attr('_index', index);
                $video.attr('videoId', sourceVideoId);
                video.poster(sourceVideoImg);
                video.src(sourceSrc);  // 重置video的src
                video.load(sourceSrc);  // 使video重新加载
            }

            clearTimeout(_this.errorTime);
            _this.errorTime = setTimeout(function () {
                console.log(video.error())
                if(video.error() !== null) {
                    if(error)error();
                }
                else {
                    video.play()
                }
            },600);

            if(fn)fn();
        },


        autoPlayTip: function (fn) {
            var _this = this;
            oAutoPlayBox.show();
            oNextTimeProcess.stop().animate({
                'width': '100%'
            }, _this.nTime*1000, function () {
                oAutoPlayBox.hide();
                oNextTimeProcess.stop().css({'width': 0});
                if(fn)fn();
            });
        },

        // 自动关闭
        autoPlayTipClose: function (start) {
            if(start) {
                oNextTimeProcess.stop().css({'width': 0});
                oAutoPlayBox.removeClass('active');
            }
            else {
                oNextTimeProcess.stop();
                oAutoPlayBox.addClass('active');
            }
        },

        checkAutoPlayNext: function (fn) {
            if(oAutoPlayBox.length > 0 && oNextTimeProcess.length > 0) {
                if(fn)fn()
            }
        }
    }


    exports.videoSet = videoSet;

});